package com.xxl.job.executor.core.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;

/**
 * 第一步:初始化加载执行器配置
 * 
 * @author duhai
 * @date 2019年3月20日
 * @see
 * @since JDK 1.8.0
 */
@Configuration
@ComponentScan(basePackages = "com.xxl.job.executor.service.jobhandler")
public class XxlJobConfig {

	private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

	// 调度中心地址
	@Value("${xxl.job.admin.addresses}")
	private String adminAddresses;

	// 执行器名称
	@Value("${xxl.job.executor.appname}")
	private String appName;

	// 执行器ip地址
	@Value("${xxl.job.executor.ip}")
	private String ip;

	// 执行器端口号
	@Value("${xxl.job.executor.port}")
	private int port;

	// token
	@Value("${xxl.job.accessToken}")
	private String accessToken;

	//日志文件目录
	@Value("${xxl.job.executor.logpath}")
	private String logPath;

	//日志保留天数
	@Value("${xxl.job.executor.logretentiondays}")
	private int logRetentionDays;

	/**
	 * 注入bean
	 * @return
	 */
	@Bean(initMethod = "start", destroyMethod = "destroy")
	public XxlJobSpringExecutor xxlJobExecutor() {
		logger.info(">>>>>>>>>>> xxl-job config init.");
		XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
		xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
		xxlJobSpringExecutor.setAppName(appName);
		xxlJobSpringExecutor.setIp(ip);
		xxlJobSpringExecutor.setPort(port);
		xxlJobSpringExecutor.setAccessToken(accessToken);
		xxlJobSpringExecutor.setLogPath(logPath);
		xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);

		return xxlJobSpringExecutor;
	}

}